﻿
var headerSearch = new function () {
    this.Keywords = ko.observable(head.Keywords);
    this.Fields = ko.observable(head.FieldId);
    this.FieldsOptions = ko.observableArray(head.SearchListFilter);

    this.Search = function() {
        RedirectLink();
    };

    // PageSize/ PageCurrent keypress enter
    this.RunSomethingKey = function (viewModel, event) {
        if (event.keyCode == 13) {
            this.RunSomething(viewModel, event);
        }
        return true;
    }
    this.RunSomething = function (viewModel, event) {
        RedirectLink();
    }

    function RedirectLink() {
        var key = $('#keywords').val();

        if (key != undefined && key != null && key != '') {
            // Partial UI loading
            openProcess();

            // Location
            var link = "/Lead/Find" + "?Keyword=" + key + "&Field=" + $('#selectFields').val();
            window.location = link;
        }
        else {
            openPopupNoSearch();
            return false;
        }
    }
};

ko.applyBindings(headerSearch, document.getElementById("headerSearch"));

function openPopupNoSearch() {
    closePopup();
    $('#fancybox-overlay').show();
    $('#popupNoSearch').showPopup({
        width: 400
    });
}

function openProcess() {
    $.fancybox($('#popup-process'), {
        padding: 0,
        closeBtn: false,
        helpers: {
            overlay: {
                opacity: 0.6,
                css: {
                    'background-color': '#11619D'
                }
            }
        },
        beforeClose: function () {
            return false;
        }
    });
    $('.fancybox-skin').show();
    $('#fancybox-overlay').show();
}

function getIndexById(id, searchListFilter) {
    for (var i = 0; i < searchListFilter.length; i++) {
        if (searchListFilter[i].ID == id) {
            return i;
        }
    }
}
function ClosePopupSearch() 
{
    $('#popupNoSearch').hide();
    $('#fancybox-overlay').hide();
    $(".fancybox-wrap").hide();
}